package com.java.base.exercise.current;

/**
 * @Author RenPu
 * @Date 2021/8/31 14:30
 * @Version 1.0
 * @Description: 俩个线程实现，交替输出奇数，偶数的打印 从0-100  实现方案1
 * 偶数:0
 * 奇数:1
 * 偶数:2
 * 奇数:3
 * 偶数:4
 * 奇数:5
 * 偶数:6
 **/
public class Thread_001 {

    private static int count = 0;
    private static final Object lock = new Object();


    public static void main(String[] args) {


        Thread thread = new Thread(new Runabled(), "偶数");
        Thread thread1 = new Thread(new Runabled(), "奇数");
        thread.start();
        thread1.start();


    }

    static class Runabled implements Runnable {

        @Override
        public void run() {
            while (count <= 100) {
                synchronized (lock) {

                    //拿到锁就打印
                    System.out.println(Thread.currentThread().getName() + ":" + count++);
                    lock.notify();
                    if (count <= 100) {
                        try {
                            lock.wait();  //防止第一个线程还没有执行完成的情况，让出锁给第一个线程
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }
                }


            }

        }
    }


}
